

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.InnerSuppleDomain;
import top.infopub.mgr.bpa.jsonVo.InnerAgrtJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.InnerSuppleService;
import top.infopub.mgr.bpa.vo.InnerSuppleVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.bpa.InnerAgrt;
import top.infopub.model.bpa.InnerSupple;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**对内合同
 * @author wuzhiyang
 * @version 2020年2月14日
 * @see InnerSuppleController
 * @since商务管理/内部协议/补充协议
 */
@Controller
@RequestMapping("/innerSupple")
public class InnerSuppleController extends GenericFlowController {

    @Autowired
    private InnerSuppleService innerSuppleService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;


    /**
     * 补充协议列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<InnerSuppleDomain> queryDataByCondition(InnerSuppleVo innerSuppleVo, ParamPage paramPage) throws ParseException {
        Page<InnerSuppleDomain> page = new Page<InnerSuppleDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (innerSuppleVo.getEndDate() != null) {
            innerSuppleVo.setEndDate(DateUtils.addDays(innerSuppleVo.getEndDate(), 1));
        }
        page = innerSuppleService.selectByDomainWhere(page, innerSuppleVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     *@Description: 内部协议-补充协议  从内部协议合同评审获取项目下拉
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/17
     */
    @RequestMapping("/queryProject")
    @ResponseBody
    public RspVo queryProject(){
        return innerSuppleService.selectProject();
    }


    /**
     *@Description: 查询详情
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/17
     */
    @RequestMapping("/queryById")
    @ResponseBody
    public RspVo queryById(String id){
        return innerSuppleService.queryById(id);
    }


    /**
     *@Description: 内部协议-补充协议 保存
     *@Param:
     *@return:
     *@Author: Tszzzzzzz
     *@date: 2021/6/17
     */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr,Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        InnerAgrtJsonVo vo = JSON.parseObject(s,InnerAgrtJsonVo.class);
        InnerSupple innerSupple = new InnerSupple();
        if (StringUtils.isNotBlank(vo.getInnerAgrt().getId())){ //编辑
            BeanUtils.copyProperties(vo.getInnerSupple(),innerSupple);
            innerSupple.setUptBy(userDtl.getId());
            innerSupple.setUptTime(new Date());
        }else{//新增
            BeanUtils.copyProperties(vo.getInnerSupple(),innerSupple);
            innerSupple.setAddBy(userDtl.getId());
            innerSupple.setAddTime(new Date());
        }
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,innerSupple.getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return innerSuppleService.saveAll(submitType,userDtl,innerSupple,vo.getInnerListings(),vo.getInnerAgrtSup(),vo.getCompPlan(),attachmentList,bodyStr);
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        InnerSupple innerSupple = innerSuppleService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (innerSupple!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), innerSupple.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(innerSupple.getProcessId()) && innerSupple.getProcessId().length() > 10){
                request.setAttribute("processId", innerSupple.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(innerSupple.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), innerSupple.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-InternalSupplementalAgreement";
    }
    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }
}
